import 'package:flutter/material.dart';

class TimeSegment {
  final String? createBy;
  final String? createTime;
  final String? updateBy;
  final String? updateTime;
  final String? remark;
  final int id;
  final int? appointmentId;
  final int seatId;
  final int? status;
  final String seatCode;
  final int? checkinStatus; // 签到状态：0-未签到，1-已签到
  final String appointmentDate;
  final String startTime;
  final String endTime;

  TimeSegment({
    this.createBy,
    this.createTime,
    this.updateBy,
    this.updateTime,
    this.remark,
    required this.id,
    this.appointmentId,
    required this.seatId,
    this.status,
    required this.seatCode,
    required this.appointmentDate,
    required this.startTime,
    required this.endTime,
    this.checkinStatus,
  });

  factory TimeSegment.fromJson(Map<String, dynamic> json) {
    return TimeSegment(
      createBy: json['createBy'],
      createTime: json['createTime'],
      updateBy: json['updateBy'],
      updateTime: json['updateTime'],
      remark: json['remark'],
      id: json['id'],
      appointmentId: json['appointmentId'],
      seatId: json['seatId'],
      status: json['status'],
      seatCode: json['seatCode'],
      appointmentDate: json['appointmentDate'],
      startTime: json['startTime'],
      endTime: json['endTime'],
      checkinStatus: json['checkinStatus'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'createBy': createBy,
      'createTime': createTime,
      'updateBy': updateBy,
      'updateTime': updateTime,
      'remark': remark,
      'id': id,
      'appointmentId': appointmentId,
      'seatId': seatId,
      'status': status,
      'seatCode': seatCode,
      'appointmentDate': appointmentDate,
      'startTime': startTime,
      'endTime': endTime,
      'checkinStatus': checkinStatus,
    };
  }

  // 获取签到状态文本
  String get checkinStatusText {
    // 使用 status 字段作为签到状态，如果没有 checkinStatus 的话
    final statusToCheck = checkinStatus ?? status ?? 0;
    switch (statusToCheck) {
      case 1:
      case 5:
        return '已签到';
      case 0:
      default:
        return '未签到';
    }
  }

  // 获取签到状态颜色
  Color get checkinStatusColor {
    // 使用 status 字段作为签到状态，如果没有 checkinStatus 的话
    final statusToCheck = checkinStatus ?? status ?? 0;
    switch (statusToCheck) {
      case 1:
        return Colors.green;
      case 0:
      default:
        return Colors.orange;
    }
  }
}

class Appointment {
  final String? createBy;
  final String? createTime;
  final String? updateBy;
  final String? updateTime;
  final String? remark;
  final int id;
  final int userId;
  final int deptId;
  final String deptName;
  final String nickName;
  final int pondId;
  final String pondName;
  final int status;
  final List<TimeSegment> timeSegments;

  Appointment({
    this.createBy,
    this.createTime,
    this.updateBy,
    this.updateTime,
    this.remark,
    required this.id,
    required this.userId,
    required this.deptId,
    required this.deptName,
    required this.nickName,
    required this.pondId,
    required this.pondName,
    required this.status,
    required this.timeSegments,
  });

  factory Appointment.fromJson(Map<String, dynamic> json) {
    return Appointment(
      createBy: json['createBy'],
      createTime: json['createTime'],
      updateBy: json['updateBy'],
      updateTime: json['updateTime'],
      remark: json['remark'],
      id: json['id'],
      userId: json['userId'],
      deptId: json['deptId'],
      deptName: json['deptName'],
      nickName: json['nickName'],
      pondId: json['pondId'],
      pondName: json['pondName'],
      status: json['status'],
      timeSegments: (json['timeSegments'] as List<dynamic>)
          .map((e) => TimeSegment.fromJson(e))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'createBy': createBy,
      'createTime': createTime,
      'updateBy': updateBy,
      'updateTime': updateTime,
      'remark': remark,
      'id': id,
      'userId': userId,
      'deptId': deptId,
      'deptName': deptName,
      'nickName': nickName,
      'pondId': pondId,
      'pondName': pondName,
      'status': status,
      'timeSegments': timeSegments.map((e) => e.toJson()).toList(),
    };
  }

  // 获取状态文本
  String get statusText {
    switch (status) {
      case 0:
        return '待确认';
      case 1:
        return '已确认';
      case 2:
        return '已取消';
      case 3:
        return '已完成';
      case 5:
        return '钓鱼中';
      default:
        return '未知状态';
    }
  }

  // 获取状态颜色
  Color get statusColor {
    switch (status) {
      case 0:
        return Colors.orange;
      case 1:
        return Colors.green;
      case 2:
        return Colors.red;
      case 3:
        return Colors.grey;
      case 5:
        return Colors.blue;
      default:
        return Colors.grey;
    }
  }
}
